package com.oocl;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * 在Java中调用js文件中的function，传入调用参数，并获取返回值
 * @author JasonWu
 * @version 
 */
public class ScriptEngineTest3 {
	public static void main(String[] args) throws IOException, ScriptException, NoSuchMethodException {
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("javascript");
		String fileName = "test.js";
		FileReader reader = new FileReader(fileName);
		engine.eval(reader);
		
		if(engine instanceof Invocable) {
			Invocable invoke = (Invocable) engine;
			Double c = (Double) invoke.invokeFunction("merge", 2, 3);
			System.out.println("c= "+c);
		}
		reader.close();
	}

}
